//
//  FFPayManager.m
//  Common
//
//  Created by IT.Bear on 16/8/20.
//  Copyright © 2016年 IT.Bear. All rights reserved.
//

#import "FFPayManager.h"
#import "FFPayHandler.h"
#import <AlipaySDK/AlipaySDK.h>
#import "FFPayCenter.h"

@interface FFPayManager ()
@property (nonatomic, strong) id<FFPayHandler> payHandler;
@property (nonatomic ,strong) FFPayInfo *payInfo;
@end
@implementation FFPayManager

- (void)goPayWithPayInfo:(FFPayInfo *)payInfo payResultBlock:(PayResult)block controller:(UIViewController *)controller
{
    self.payResultBlock = block;
    
    self.payHandler = [[FFPayCenter sharedInstance] buildPayHandlerWith:payInfo viewController:controller];
    //支付结果回调block
    [[FFPayCenter sharedInstance] setResultAction:^(FFPayResult result) {
        
        switch (result) {
            case FFPayResultAlixpaySuccess:
            {
                [self paySuccess];
            }
                break;
            case FFPayResultAlixpayFailed:
            {
                [self payFailed];
            }
                break;
            case FFPayResultAlixpayCancel:
            {
                [self payFailed];
            }
                break;
            case FFPayResultWeChatPaySuccess:
            {
                [self paySuccess];
            }
                break;
            case FFPayResultWeChatPayFailed:
            {
                [self payFailed];
            }
                break;
            case FFPayResultWeChatPayCancel:
            {
                [self payFailed];
            }
                break;
            case FFPayResultUnionPaySuccess:
            {
                [self paySuccess];
            }
                break;
            case FFPayResultUnionPayFailed:
            {
                [self payFailed];
            }
                break;
            case FFPayResultUnionPayCancel:
            {
                [self payFailed];
            }
                break;
                
            case FFPayResultMimePaySuccess:
            {
                [self paySuccess];
            }
                break;
            case FFPayResultMimePayFailed:
            {
                [self payFailed];
            }
                break;
            case FFPayResultMimePayCancel:
            {
                [self payFailed];
            }
                break;
            default:
                break;
        }
    }];
    //支付
    [self.payHandler goPay];
}

- (void)paySuccess {
    if (self.payResultBlock) {
        self.payResultBlock(YES);
    }
}

- (void)payFailed {
    if (self.payResultBlock) {
        self.payResultBlock(NO);
    }
}


@end
